﻿using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;

namespace Leo.Awesome.Infrastructure.Data;

public class InfrastructureDbMigrationService : ITransientDependency
{
    public ILogger<InfrastructureDbMigrationService> Logger { get; set; }

    private readonly IDataSeeder _dataSeeder;
    private readonly IEnumerable<IInfrastructureDbSchemaMigrator> _dbSchemaMigrators;
    //private readonly ITenantRepository _tenantRepository;
    private readonly ICurrentTenant _currentTenant;

    public InfrastructureDbMigrationService(
        IDataSeeder dataSeeder,
        IEnumerable<IInfrastructureDbSchemaMigrator> dbSchemaMigrators,
        //ITenantRepository tenantRepository,
        ICurrentTenant currentTenant)
    {
        _dataSeeder = dataSeeder;
        _dbSchemaMigrators = dbSchemaMigrators;
        //_tenantRepository = tenantRepository;
        _currentTenant = currentTenant;

        Logger = NullLogger<InfrastructureDbMigrationService>.Instance;
    }

    public async Task MigrateAsync()
    {

        Logger.LogInformation("Started database migrations...");

        await MigrateDatabaseSchemaAsync();
        await SeedDataAsync();

        Logger.LogInformation($"Successfully completed host database migrations.");

        //var tenants = await _tenantRepository.GetListAsync(includeDetails: true);

        //var migratedDatabaseSchemas = new HashSet<string>();
        //foreach (var tenant in tenants)
        //{
        //    using (_currentTenant.Change(tenant.Id))
        //    {
        //        if (tenant.ConnectionStrings.Any())
        //        {
        //            var tenantConnectionStrings = tenant.ConnectionStrings
        //                .Select(x => x.Value)
        //                .ToList();

        //            if (!migratedDatabaseSchemas.IsSupersetOf(tenantConnectionStrings))
        //            {
        //                await MigrateDatabaseSchemaAsync(tenant);

        //                migratedDatabaseSchemas.AddIfNotContains(tenantConnectionStrings);
        //            }
        //        }

        //        await SeedDataAsync(tenant);
        //    }

        //    Logger.LogInformation($"Successfully completed {tenant.Name} tenant database migrations.");
        //}

        Logger.LogInformation("Successfully completed all database migrations.");
        Logger.LogInformation("You can safely end this process...");
    }

    private async Task MigrateDatabaseSchemaAsync()
    {
        foreach (var migrator in _dbSchemaMigrators)
        {
            await migrator.MigrateAsync();
        }
    }

    private async Task SeedDataAsync()
    {
        //Logger.LogInformation($"Executing {(tenant == null ? "host" : tenant.Name + " tenant")} database seed...");

        //await _dataSeeder.SeedAsync(new DataSeedContext(tenant?.Id)
        //    .WithProperty(IdentityDataSeedContributor.AdminEmailPropertyName, IdentityDataSeedContributor.AdminEmailDefaultValue)
        //    .WithProperty(IdentityDataSeedContributor.AdminPasswordPropertyName, IdentityDataSeedContributor.AdminPasswordDefaultValue)
        //);
        await _dataSeeder.SeedAsync();
    }

}
